﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoGo3
{
    /// <summary>
    /// Bar类
    /// </summary>
    class Bar
    {
        public Rectangle Rectangle { get; set; }
        private const int Step = 10;
        private const int BarWidth = 100;
        private int _gameWidth;


        public Bar(int gameWidth)
        {
            _gameWidth = gameWidth;
            Rectangle = new Rectangle((_gameWidth-BarWidth)/2, 350, BarWidth,15);
        }
        /// <summary>
        /// 绘制Bar
        /// </summary>
        /// <param name="g"></param>

        public void Draw(Graphics g)
        {
            g.FillRectangle(new SolidBrush(Color.Blue), Rectangle);
        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>

        public void KeyDown(String key)
        {
            int newX = Rectangle.X;

            if (key == "A")
            {
                newX -= Step;
                //防止左出界
                if (newX < 0)
                    newX = 0;
            }

            if (key == "D")
            {
                newX += Step;
                if (newX + BarWidth > _gameWidth)
                    newX = _gameWidth - BarWidth;
            }
                
                Rectangle = new Rectangle(newX,
                                          Rectangle.Y,
                                          Rectangle.Width,
                                          Rectangle.Height);
        }
    }
}
